package cn.nawang.ebeim.server.thread;

import cn.nawang.ebeim.server.constants.Config;
import cn.nawang.ebeim.server.constants.Constant;
import cn.nawang.ebeim.server.entity.StorageFileToDo;
import cn.nawang.ebeim.server.queue.EbeimQueues;
import cn.nawang.ebeim.server.task.UploadFileTask;
import cn.nawang.ebeim.server.util.ApiVisitUtil;
import cn.nawang.ebeim.server.util.TransportUtil;

import java.io.File;

/**
 * Created by GanJc on 2015-11-11 10:39
 */
public class FileUploadFolderScanner extends FolderScanner {

    @Override
    protected String getScanPath() {
        return  Config.WORKING_DIR + Config.FILE_UPLOAD;
    }

    @Override
    protected String getFileType() {
        return  Constant.FILE_TYPE_FILE;
    }

    @Override
    protected void putIntoTaskQueue(UploadFileTask uploadFileTask) {
        try {
            if (EbeimQueues.uploadFileQueue.contains(uploadFileTask)){
                LOG.info("文件已存在队列:{}" , uploadFileTask );
            }else {
                EbeimQueues.uploadFileQueue.put(uploadFileTask);
                LOG.info("将文件放入上传队列:{}" , uploadFileTask );
            }
        } catch (InterruptedException e) {
            LOG.error("将文件放入上传队列失败:{}", e);
        }
    }

    @Override
    protected void redoBreakPointTask(String dsId, File file, String trueFileName, String tempFileName) {
        LOG.info("断点续传文件：" + file.getName());
        StorageFileToDo storageFileToDo = ApiVisitUtil.getStorageFileToDoByDsIdAndSignature(dsId, trueFileName);
        boolean renameToFlag = file.renameTo(new File(tempFileName));
        LOG.info("断点续传开始前重命名为临时文件结果：{} - " + renameToFlag, tempFileName);
        TransportUtil.downloadDataFile(dsId, trueFileName, storageFileToDo.getStorageCloudId1());
    }

}
